﻿Imports NewHeaderSystems.Archive.Engine

Public Class DownloadConfirms
    Inherits PageTemplate

    Private _isBusy As Boolean = False

    Private Sub DownloadConfirms_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Title = "Download Confirms"
        Description = "Click on the Start button to retrieve new confirm files from your APL directory."
        PageIcon = My.Resources.Control_Panel

    End Sub

    Private Sub btnStartDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartDownload.Click
        SetFormStateToBusy(True)
        If HasChanged() Then
            With SystemSettings.Instance
                .FtpServer = txtFtpServer.Text
                .FtpUsername = txtAplUserID.Text
                .FtpPassword = txtAplPassword.Text
                .FtpChangeDirectory = txtChangeDirectory.Text
                .Save()
            End With
        End If
        backgroundWorker.RunWorkerAsync()
    End Sub

    Private Sub backgroundWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles backgroundWorker.DoWork
        Dim ftp As New FtpDownloader(backgroundWorker)
        e.Result = ftp.DownloadMatchingFiles()
    End Sub

    Private Sub backgroundWorker_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles backgroundWorker.ProgressChanged
        lblStatus.Text = e.UserState.ToString()
        pbProgress.Value = e.ProgressPercentage
        Application.DoEvents()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If _isBusy Then
            backgroundWorker.CancelAsync()
        Else 'User wants to go to previous page (either home or Start Archiving)
            GoBack()
        End If
    End Sub

    Private Sub SetFormStateToBusy(ByVal isBusy As Boolean)
        _isBusy = isBusy
        btnStartDownload.Enabled = Not isBusy
        lblStatus.Visible = isBusy
        pbProgress.Visible = isBusy
    End Sub

    Private Sub backgroundWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles backgroundWorker.RunWorkerCompleted
        SetFormStateToBusy(False)
        Dim count As Integer
        Integer.TryParse(e.Result.ToString(), count)

        If Not e.Error Is Nothing Then
            Dim message As String = "An error occured while downloading." + Environment.NewLine + e.Error.Message
            MessageBox.Show(message, "Download Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        ElseIf count = -1 Then 'User cancelled
            'Don't do anything
        Else
            If count = 0 Then
                MessageBox.Show("There were no files available for download.", "No Files To Download", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show(count.ToString() + " files were downloaded successfully.", "Download Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

            GoToPage(FormTypes.StartArchiving)
        End If
    End Sub

    Public Overrides Function CanLeave() As Boolean
        Return Not _isBusy 'Don't allow user to leave if downloading
    End Function

    Private Sub DownloadConfirms_PageEnter(ByVal sender As System.Object, ByVal e As Microsoft.Samples.Windows.Forms.Navigation.PageEventArgs) Handles MyBase.PageEnter
        SetFormStateToBusy(False) 'Set initial state
        With SystemSettings.Instance
            txtFtpServer.Text = .FtpServer
            txtAplUserID.Text = .FtpUsername
            txtAplPassword.Text = .FtpPassword
            txtChangeDirectory.Text = .FtpChangeDirectory
        End With
    End Sub

    Private Function HasChanged() As Boolean
        With SystemSettings.Instance
            Return txtFtpServer.Text <> .FtpServer Or _
            txtAplUserID.Text <> .FtpUsername Or _
            txtAplPassword.Text <> .FtpPassword Or _
            txtChangeDirectory.Text <> .FtpChangeDirectory
        End With
    End Function

End Class
